

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>geoslurp.dataset.motuGridsBase &mdash; geoslurp v1.1.0 documentation</title>
  

  
  
    <link rel="shortcut icon" href="../../../_static/favicon.ico"/>
  
  
  

  

  
  
    

  

  
  
    <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  

  

  
        <link rel="index" title="Index"
              href="../../../genindex.html"/>
        <link rel="search" title="Search" href="../../../search.html"/>
    <link rel="top" title="geoslurp v1.1.0 documentation" href="../../../index.html"/>
        <link rel="up" title="Module code" href="../../index.html"/> 

  
  <script src="../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav" role="document">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../index.html" class="icon icon-home"> geoslurp
          

          
          </a>

          
            
            
              <div class="version">
                1.1.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../introduction.html">An introduction to Geoslurp</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../installation.html">Installation of the geoslurp package and setting up the PostgreSQL instance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../confsettings.html">Configuration</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../examples.html">Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../reference.html">Reference Documentation</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">geoslurp</a>
        
      </nav>


      
      <div class="wy-nav-content">
        <div class="rst-content">
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>geoslurp.dataset.motuGridsBase</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for geoslurp.dataset.motuGridsBase</h1><div class="highlight"><pre>
<span></span><span class="c1"># This file is part of geoslurp.</span>
<span class="c1"># geoslurp is free software; you can redistribute it and/or</span>
<span class="c1"># modify it under the terms of the GNU Lesser General Public</span>
<span class="c1"># License as published by the Free Software Foundation; either</span>
<span class="c1"># version 3 of the License, or (at your option) any later version.</span>

<span class="c1"># geoslurp is distributed in the hope that it will be useful,</span>
<span class="c1"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c1"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU</span>
<span class="c1"># Lesser General Public License for more details.</span>

<span class="c1"># You should have received a copy of the GNU Lesser General Public</span>
<span class="c1"># License along with Frommle; if not, write to the Free Software</span>
<span class="c1"># Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA</span>

<span class="c1"># Author Roelof Rietbroek (roelof@geod.uni-bonn.de), 2018</span>

<span class="kn">from</span> <span class="nn">geoslurp.dataset</span> <span class="k">import</span> <span class="n">DataSet</span>
<span class="kn">from</span> <span class="nn">geoslurp.datapull.motu</span> <span class="k">import</span> <span class="n">Uri</span> <span class="k">as</span> <span class="n">MotuUri</span>
<span class="kn">from</span> <span class="nn">geoslurp.datapull.motu</span> <span class="k">import</span> <span class="n">MotuOpts</span><span class="p">,</span> <span class="n">MotuRecursive</span>
<span class="kn">from</span> <span class="nn">geoslurp.tools.Bounds</span> <span class="k">import</span> <span class="n">BtdBox</span>
<span class="kn">from</span> <span class="nn">geoslurp.tools.netcdftools</span> <span class="k">import</span> <span class="n">ncSwapLongitude</span><span class="p">,</span><span class="n">stackNcFiles</span>
<span class="kn">from</span> <span class="nn">geoslurp.config.catalogue</span> <span class="k">import</span> <span class="n">geoslurpCatalogue</span>

<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">geoalchemy2.types</span> <span class="k">import</span> <span class="n">Geography</span>
<span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="k">import</span> <span class="n">Column</span><span class="p">,</span><span class="n">Integer</span><span class="p">,</span><span class="n">String</span><span class="p">,</span> <span class="n">Boolean</span><span class="p">,</span><span class="n">ARRAY</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.dialects.postgresql</span> <span class="k">import</span> <span class="n">TIMESTAMP</span><span class="p">,</span> <span class="n">JSONB</span>
<span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="k">import</span> <span class="n">MetaData</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="k">import</span> <span class="n">declarative_base</span>
<span class="kn">from</span> <span class="nn">netCDF4</span> <span class="k">import</span> <span class="n">Dataset</span> <span class="k">as</span> <span class="n">ncDset</span>

<span class="kn">from</span> <span class="nn">netCDF4</span> <span class="k">import</span> <span class="n">num2date</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">datetime</span><span class="p">,</span><span class="n">timedelta</span>
<span class="kn">from</span> <span class="nn">geoslurp.datapull</span> <span class="k">import</span> <span class="n">findFiles</span><span class="p">,</span><span class="n">UriFile</span>
<span class="kn">from</span> <span class="nn">geoslurp.config.slurplogger</span> <span class="k">import</span> <span class="n">slurplogger</span>
<span class="kn">from</span> <span class="nn">geoslurp.dataset.RasterBase</span> <span class="k">import</span> <span class="n">RasterBase</span>
<span class="kn">import</span> <span class="nn">copy</span>

<div class="viewcode-block" id="MotuGridsBase"><a class="viewcode-back" href="../../../reference/geoslurp.dataset.html#geoslurp.dataset.motuGridsBase.MotuGridsBase">[docs]</a><span class="k">class</span> <span class="nc">MotuGridsBase</span><span class="p">(</span><span class="n">RasterBase</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Downloads and register subsets of gridded data with the motu client&quot;&quot;&quot;</span>
    <span class="n">outofdb</span><span class="o">=</span><span class="kc">True</span>
    <span class="n">rastregex</span><span class="o">=</span><span class="s2">&quot;\.nc$&quot;</span>
    <span class="n">auxcolumns</span><span class="o">=</span><span class="p">[</span><span class="n">Column</span><span class="p">(</span><span class="s2">&quot;lastupdate&quot;</span><span class="p">,</span><span class="n">TIMESTAMP</span><span class="p">),</span><span class="n">Column</span><span class="p">(</span><span class="s2">&quot;time&quot;</span><span class="p">,</span><span class="n">ARRAY</span><span class="p">(</span><span class="n">TIMESTAMP</span><span class="p">))]</span>
    <span class="n">updated</span><span class="o">=</span><span class="p">[]</span>
    <span class="n">moturoot</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">motuservice</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">motuproduct</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">authalias</span><span class="o">=</span><span class="kc">None</span>
    <span class="c1">#the variable of interest to retrieve</span>
    <span class="n">variables</span><span class="o">=</span><span class="kc">None</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">dbconn</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">dbconn</span><span class="p">)</span>

<div class="viewcode-block" id="MotuGridsBase.pull"><a class="viewcode-back" href="../../../reference/geoslurp.dataset.html#geoslurp.dataset.motuGridsBase.MotuGridsBase.pull">[docs]</a>    <span class="k">def</span> <span class="nf">pull</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">wsne</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span><span class="n">tstart</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span><span class="n">tend</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Pulls a subset of a gridded dataset as netcdf from an motu enabled server</span>
<span class="sd">        This routine calls the internal routines of the motuclient python client</span>
<span class="sd">        :param name: Name of the  output datatset (file will be named &#39;name.nc&#39;)</span>
<span class="sd">        :param wsne: bounding box of the section of interest as [West,South,North,East]</span>
<span class="sd">        :param tstart: start date (as yyyy-mm-dd) for the extraction</span>
<span class="sd">        :param tend: end date (as yyyy-mm-dd) for the extraction</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">name</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;A name must be supplied to MotuGridsBase.pull !!&quot;</span><span class="p">)</span>


        <span class="k">if</span> <span class="kc">None</span> <span class="ow">in</span> <span class="n">wsne</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Please supply a geographical bounding box&quot;</span><span class="p">)</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="n">bbox</span><span class="o">=</span><span class="n">BtdBox</span><span class="p">(</span><span class="n">w</span><span class="o">=</span><span class="n">wsne</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="n">n</span><span class="o">=</span><span class="n">wsne</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span><span class="n">s</span><span class="o">=</span><span class="n">wsne</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span><span class="n">e</span><span class="o">=</span><span class="n">wsne</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span><span class="n">ts</span><span class="o">=</span><span class="n">tstart</span><span class="p">,</span><span class="n">te</span><span class="o">=</span><span class="n">tend</span><span class="p">)</span>
        <span class="k">except</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Invalid bounding box provided to Duacs pull&quot;</span><span class="p">)</span>


        <span class="n">cred</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">conf</span><span class="o">.</span><span class="n">authCred</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">authalias</span><span class="p">)</span>
        <span class="n">ncout</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dataDir</span><span class="p">(),</span><span class="n">name</span><span class="o">+</span><span class="s2">&quot;.nc&quot;</span><span class="p">)</span>

        <span class="n">mOpts</span><span class="o">=</span><span class="n">MotuOpts</span><span class="p">(</span><span class="n">moturoot</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">moturoot</span><span class="p">,</span><span class="n">service</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">motuservice</span><span class="p">,</span>
                       <span class="n">product</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">motuproduct</span><span class="p">,</span><span class="n">btdbox</span><span class="o">=</span><span class="n">bbox</span><span class="p">,</span><span class="n">fout</span><span class="o">=</span><span class="n">ncout</span><span class="p">,</span><span class="n">cache</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">cacheDir</span><span class="p">(),</span><span class="n">variables</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">variables</span><span class="p">,</span><span class="n">auth</span><span class="o">=</span><span class="n">cred</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">bbox</span><span class="o">.</span><span class="n">isGMTCentered</span><span class="p">():</span>
            <span class="c1"># we need 2 downloads to split the  and a merging of the grids !</span>
            <span class="c1"># split the bounding box in two</span>
            <span class="n">bboxleft</span><span class="p">,</span><span class="n">bboxright</span><span class="o">=</span><span class="n">bbox</span><span class="o">.</span><span class="n">lonSplit</span><span class="p">(</span><span class="mf">0.0</span><span class="p">)</span>
            <span class="n">bboxleft</span><span class="o">.</span><span class="n">to0_360</span><span class="p">()</span>
            <span class="n">bboxright</span><span class="o">.</span><span class="n">to0_360</span><span class="p">()</span>

            <span class="n">ncoutleft</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cacheDir</span><span class="p">(),</span><span class="n">name</span><span class="o">+</span><span class="s2">&quot;_left.nc&quot;</span><span class="p">)</span>

            <span class="n">mOptsleft</span><span class="o">=</span><span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">mOpts</span><span class="p">)</span>
            <span class="n">mOptsleft</span><span class="o">.</span><span class="n">syncbtdbox</span><span class="p">(</span><span class="n">bboxleft</span><span class="p">)</span>
            <span class="n">mOptsleft</span><span class="o">.</span><span class="n">syncfilename</span><span class="p">(</span><span class="n">ncoutleft</span><span class="p">)</span>

            <span class="n">MotuRecleft</span><span class="o">=</span><span class="n">MotuRecursive</span><span class="p">(</span><span class="n">mOptsleft</span><span class="p">)</span>
            <span class="n">urileft</span><span class="p">,</span><span class="n">updleft</span><span class="o">=</span><span class="n">MotuRecleft</span><span class="o">.</span><span class="n">download</span><span class="p">()</span>

            <span class="n">ncoutright</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cacheDir</span><span class="p">(),</span><span class="n">name</span><span class="o">+</span><span class="s2">&quot;_right.nc&quot;</span><span class="p">)</span>
            <span class="n">mOptsright</span><span class="o">=</span><span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">mOpts</span><span class="p">)</span>
            <span class="n">mOptsright</span><span class="o">.</span><span class="n">syncbtdbox</span><span class="p">(</span><span class="n">bboxright</span><span class="p">)</span>
            <span class="n">mOptsright</span><span class="o">.</span><span class="n">syncfilename</span><span class="p">(</span><span class="n">ncoutright</span><span class="p">)</span>

            <span class="n">MotuRecright</span><span class="o">=</span><span class="n">MotuRecursive</span><span class="p">(</span><span class="n">mOptsright</span><span class="p">)</span>
            <span class="n">uriright</span><span class="p">,</span><span class="n">updright</span><span class="o">=</span><span class="n">MotuRecright</span><span class="o">.</span><span class="n">download</span><span class="p">()</span>

            <span class="n">stackNcFiles</span><span class="p">(</span><span class="n">ncout</span><span class="p">,</span><span class="n">urileft</span><span class="o">.</span><span class="n">url</span><span class="p">,</span><span class="n">uriright</span><span class="o">.</span><span class="n">url</span><span class="p">,</span><span class="s1">&#39;longitude&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">updleft</span> <span class="ow">or</span> <span class="n">updright</span><span class="p">:</span>
                <span class="c1">#change the longitude representation to -180..0 (without reshuffeling the data</span>
                <span class="n">ncSwapLongitude</span><span class="p">(</span><span class="n">urileft</span><span class="o">.</span><span class="n">url</span><span class="p">)</span>
                <span class="c1"># patch files</span>
                <span class="n">uri</span><span class="p">,</span><span class="n">upd</span><span class="o">=</span><span class="n">stackNcFiles</span><span class="p">(</span><span class="n">ncout</span><span class="p">,</span><span class="n">urileft</span><span class="o">.</span><span class="n">url</span><span class="p">,</span><span class="n">uriright</span><span class="o">.</span><span class="n">url</span><span class="p">,</span><span class="s1">&#39;longitude&#39;</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">upd</span><span class="o">=</span><span class="kc">False</span>
                <span class="n">uri</span><span class="o">=</span><span class="n">UriFile</span><span class="p">(</span><span class="n">ncout</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1">#we can handle this by a single recursive motu instance</span>

            <span class="n">MotuRec</span><span class="o">=</span><span class="n">MotuRecursive</span><span class="p">(</span><span class="n">mOpts</span><span class="p">)</span>
            <span class="n">uri</span><span class="p">,</span><span class="n">upd</span><span class="o">=</span><span class="n">MotuRec</span><span class="o">.</span><span class="n">download</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">upd</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">updated</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span></div>

<div class="viewcode-block" id="MotuGridsBase.rastExtract"><a class="viewcode-back" href="../../../reference/geoslurp.dataset.html#geoslurp.dataset.motuGridsBase.MotuGridsBase.rastExtract">[docs]</a>    <span class="k">def</span> <span class="nf">rastExtract</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">uri</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;extract raster and other meta info from the downloaded files&quot;&quot;&quot;</span>
        <span class="n">meta</span><span class="o">=</span><span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">rastExtract</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span>
        <span class="n">meta</span><span class="p">[</span><span class="s2">&quot;lastupdate&quot;</span><span class="p">]</span><span class="o">=</span><span class="n">uri</span><span class="o">.</span><span class="n">lastmod</span>
        <span class="n">ncid</span><span class="o">=</span><span class="n">ncDset</span><span class="p">(</span><span class="n">uri</span><span class="o">.</span><span class="n">url</span><span class="p">)</span>
        <span class="n">time</span><span class="o">=</span><span class="n">ncid</span><span class="o">.</span><span class="n">variables</span><span class="p">[</span><span class="s2">&quot;time&quot;</span><span class="p">]</span>
        <span class="c1"># import pdb;pdb.set_trace()</span>
        <span class="c1"># t0=datetime(1950,1,1)</span>
        <span class="n">meta</span><span class="p">[</span><span class="s2">&quot;time&quot;</span><span class="p">]</span><span class="o">=</span><span class="n">num2date</span><span class="p">(</span><span class="n">time</span><span class="p">[:],</span><span class="n">time</span><span class="o">.</span><span class="n">units</span><span class="p">)</span>
        <span class="c1"># s=int(x),seconds=int(86400*divmod(x,int(x))[1])) for x in ncid[&#39;time&#39;][:]]</span>
        <span class="k">return</span> <span class="n">meta</span></div></div>

     
</pre></div>

           </div>
           <div class="articleComments">
            
           </div>
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, Roelof Rietbroek.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../',
            VERSION:'v1.1.0',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../_static/doctools.js"></script>

  

  
  
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>
  

  
  
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.StickyNav.enable();
      });
  </script>
   

</body>
</html>